<template>
<!--<q-dialog class="add-edit-station-dialog dialog" v-model="show" persistent>-->
<!--  <q-card v-drag style="min-width: 400px">-->
<!--    <q-card-section class="dialog-title bg-primary">-->
<!--      <div class="text-center text-white">{{state==='add'?'添加部门':state==='child'?'添加子项':'编辑部门'}}</div>-->
<!--    </q-card-section>-->
  <basic-dialog :visible="show"
                :title="state==='add'?'添加部门':state==='child'?'添加子项':'编辑部门'"
                min-width="400px"
  >
    <q-form @submit="submit" class="dialog-content">
    <div class="padding-t-20 padding-l-20 padding-r-20">
      <yz-input v-model="name"
                :rules="[0]"
                :maxlength="20"
                title="部门名称"/>
      <yz-input v-model="parentName"
                is-disable
                title="上级部门"/>
<!--      <yz-input v-model="departAttr"-->
<!--                inputType="select"-->
<!--                :options="orgList"-->
<!--                :rules="[0]"-->
<!--                title="部门属性"/>-->
<!--      <div class="dialog-item flex items-start" style="margin: -10px 0 10px">-->
<!--        <label class="text-right name radio-name">是否班组:</label>-->
<!--        <div>-->
<!--          <q-radio v-model="isTeam" :val="1" label="是" :disable="teamDisable" />-->
<!--          <q-radio v-model="isTeam" :val="0" label="否" :disable="teamDisable" />-->
<!--        </div>-->
<!--      </div>-->
<!--      <yz-input-->
<!--        v-model.trim="remark"-->
<!--        inputType="textarea"-->
<!--        :maxlength="200"-->
<!--        title="备注"-->
<!--      />-->
    </div>
    <q-separator />
    <div class="dialog-footer text-center margin-t-10">
      <q-btn outline label="取消" color="primary" @click="hideDialog" class="q-mr-md"/>
      <q-btn unelevated label="确认" color="primary" type="submit" :loading="submitting"/>
    </div>
  </q-form>
  </basic-dialog>
</template>

<script>
export default {
  name: 'add-edit-station-dialog',
  props: {
    // 组织层级列表
    orgList: {
      type: Array
    }
  },
  data () {
    return {
      show: false,
      // 分为编辑状态和添加状态 'code-edit' / 'add' / 'child'
      state: 'add',
      name: '', // 所在位置
      parentName: '', // 上级名称
      parentId: '', // 上级名称
      departAttr: null,
      isTeam: 0, // 0：否 1：是
      teamDisable: false,
      remark: '', // 备注
      id: null, // 工位id
      submitting: false // 提交状态
    }
  },
  methods: {
    // 显示弹框
    showDialog (state, item) {
      this.initInfo(state, item)
      this.show = true
    },
    // 隐藏弹框
    hideDialog () {
      this.show = false
    },
    // 初始化数据
    initInfo (state, info) {
      this.state = state
      if (state === 'add') {
        this.name = ''
        this.parentName = '/'
        this.parentId = 0
        this.departAttr = this.orgList.length > 0 ? this.orgList[0].value : null
        this.remark = ''
        this.isTeam = 0
        this.id = null
      } else {
        this.name = info.label
        this.parentName = info.parentName
        this.parentId = info.parentId
        this.departAttr = info.type

        this.isTeam = info.teamFlag
        this.remark = info.remark
        this.id = info.id
      }
      if (state === 'edit') {
        this.teamDisable = info.childrenCount !== 0
      }
    },
    // 提交
    submit () {
      const data = {
        name: this.name,
        type: this.departAttr,
        teamFlag: this.isTeam,
        remark: this.remark,
        sorting: 0
      }
      if (this.state === 'edit') {
        data.id = this.id
      }
      if (this.state !== 'add') {
        data.parentId = this.parentId
      }
      this.submitting = true
      this.$emit('submit', this.state, data)
    }
  }
}
</script>

<style scoped lang="stylus">
.dialog{
}
</style>
